'use strict';
import * as Koa from 'koa';
import { router, prefix } from '../middleware/router';
import { customer_accountService as userService } from '../service/customer_account';
import { response, download } from '../middleware/result';
import { invitationService } from '../service/invitation';

/** 小程序用户API对象 */
@prefix('user')
class Customer_accountController {
    /**
    * 保存小程序用户信息
    * @api {post} /user/save 保存小程序用户信息
    * @apiDescription 保存小程序用户信息
    * @apiName save
    * @apiParam (参数) {String} token  用户标识token
    * @apiParam (参数) {String} nickname  昵称
    * @apiParam (参数) {String} phone  手机号
    * @apiParam (参数) {String} ivtercode 邀请人标识code
    * @apiParam (参数) {String} device  设备信息
    * @apiParam (参数) {String} headimg  头像图片
    * @apiParam (参数) {String} sex  性别
    * @apiParam (参数) {String} longitude 经度
    * @apiParam (参数) {String} latitude 纬度
    * @apiParam (参数) {String} ad_info   {adcode：城市编码，province：省份，city：城市，district：区域}   所在地信息
    * @apiSampleRequest /user/save
    * @apiGroup User
    * @apiVersion 3.0.0
    */
    @router('/save', { method: 'post' })
    async save(ctx: Koa.Context) {
        let result = await userService.save(ctx);
        response(ctx, result);
    }
    /**
    * 禁用用户
    * @api {get} /user/lock 禁用用户(PC)
    * @apiDescription 禁用用户
    * @apiName lock
    * @apiParam (参数) {String} userid  用户id
    * @apiSampleRequest /user/lock
    * @apiGroup User
    * @apiVersion 1.0.0
    */
    @router('/lock/:userid')
    async lock(ctx: Koa.Context) {
        let result = await userService.lock(ctx.params.userid);
        response(ctx, result);
    }
    /**
    * 启用用户
    * @api {get} /user/open 启用用户(PC)
    * @apiDescription 启用用户
    * @apiName open
    * @apiParam (参数) {String} userid  用户id
    * @apiSampleRequest /user/open
    * @apiGroup User
    * @apiVersion 1.0.0
    */
    @router('/open/:userid')
    async open(ctx: Koa.Context) {
        let result = await userService.open(ctx.params.userid);
        response(ctx, result);
    }
    @router('/setsender/:userid')
    async setsender(ctx: Koa.Context) {
        let result = await userService.setsender(ctx.params.userid);
        response(ctx, result);
    }
    @router('/nosender/:userid')
    async nosender(ctx: Koa.Context) {
        let result = await userService.nosender(ctx.params.userid);
        response(ctx, result);
    }
    /**
    * 获取用户信息
    * @api {post} /user/queryById 获取用户信息
    * @apiDescription 获取用户信息
    * @apiName queryById
    * @apiParam (参数) {String} token  用户标识token
    * @apiSampleRequest /user/queryById
    * @apiGroup User
    * @apiVersion 1.0.0
    */
    @router('/queryById', { method: 'post' })
    async queryById(ctx: Koa.Context) {
        let result = await userService.queryinfo(ctx.request.body.token);
        response(ctx, result);
    }
    /**
    * 白金会员领取礼品
    * @api {post} /user/getGift 白金会员领取礼品
    * @apiDescription 白金会员领取礼品
    * @apiName getGift
    * @apiParam (参数) {String} token  用户标识token
    * @apiSampleRequest /user/getGift
    * @apiGroup User
    * @apiVersion 1.0.0
    */
   @router('/getGift', { method: 'post' })
   async getGift(ctx: Koa.Context) {
       let result = await userService.getGift(ctx.request.body.token);
       response(ctx, result);
   }
   @router('/getGift2', { method: 'post' })
   async getGift2(ctx: Koa.Context) {
       let result = await userService.getGift2(ctx.request.body.token);
       response(ctx, result);
   }
   @router('/getGift3', { method: 'post' })
   async getGift3(ctx: Koa.Context) {
       let result = await userService.getGift3(ctx.request.body.token);
       response(ctx, result);
   }
    /**
     * 
     * @api {post} /user/query  查询用户(PC)
     * @apiDescription 查询用户
     * @apiName query
     * @apiParam (参数) {String} offset   页码(第一页为1)
     * @apiParam (参数) {String} limit    每页条数
     * @apiParam (参数) {String} name    用户名/手机号
     * @apiParam (参数) {String} status  用户状态
     * @apiParam (参数) {String} regstarttime   注册日期起始日期
     * @apiParam (参数) {String} regendtime   注册日期截止日期
     * @apiParam (参数) {String} upstarttime   升级日期起始日期
     * @apiParam (参数) {String} upendtime   升级日期截止日期
     * @apiSampleRequest /user/query
     * @apiGroup User
     * @apiVersion 1.0.0
     */
    @router('/query', { method: 'post' })
    async query(ctx: Koa.Context) {
        let result = await userService.query(ctx.request.body);
        response(ctx, result);
    }
    @router('/export', { method: 'post' })
    async export(ctx: Koa.Context) {
        let result = await userService.export(ctx.request.body);
        download(ctx, result);
    }

    /**
    * 发送验证码
    * @api {post} /user/sendsms 发送验证码
    * @apiDescription 发送验证码
    * @apiName sendsms
    * @apiParam (参数) {String} token  用户标识token
    * @apiParam (参数) {String} phone 
    * @apiParam (参数) {String} type 1.身份验证 2.登录确认 3.用户注册 4.修改密码
    * @apiSampleRequest /user/sendsms
    * @apiGroup User
    * @apiVersion 2.0.0
    */
    @router('/sendsms', { method: 'post' })
    async sendsms(ctx: Koa.Context) {
        let token = ctx.request.body.token;
        let phone = ctx.request.body.phone;
        let type = ctx.request.body.type;
        let res: any = await userService.sendsms(token, phone, type);
        response(ctx, res);
    }
    /**
     * 手机号登录
     * @api {post} /user/login 手机号登录
     * @apiDescription 手机号登录
     * @apiName login
     * @apiParam (参数) {String} token  用户标识token
     * @apiParam (参数) {String} phone  手机号
     * @apiParam (参数) {String} valite 6位验证码
     * @apiSampleRequest /user/login
     * @apiGroup User
     * @apiVersion 4.0.0
     */
    @router('/login', { method: 'post' })
    async login(ctx: Koa.Context) {
        let token = ctx.request.body.token;
        let phone = ctx.request.body.phone;
        let valite = ctx.request.body.valite;
        let res: any = await userService.login(phone, valite, token);
        response(ctx, res);
    }

    @router('/queryinver', { method: 'post' })
    async queryinver(ctx: Koa.Context) {
        let res: any = await invitationService.querylist(ctx.request.body);
        response(ctx, res);
    }

    //查询品牌宣传大使邀请的用户
    //  @router('/queryagentcustomer', { method: 'post' })
    //  async queryagentcustomer(ctx: Koa.Context) {
    //      let res: any = await userService.queryagentcustomer(ctx.request.body);
    //      response(ctx, res);
    //  }

    @router('/queryoneuser', { method: 'post' })
    async queryoneuser(ctx: Koa.Context) {
        let res: any = await userService.querySingle({ _id: ctx.request.body._id });
        response(ctx, res);
    }

    @router('/initdate')
    async initdate(ctx: Koa.Context) {
        await userService.initdate()
        response(ctx, 'ok');
    }

    @router('/upwine', { method: 'post' })
    async upwine(ctx: Koa.Context) {
        await userService.upwine(ctx.request.body);
        response(ctx, 'ok');
    }

    @router('/getuserinfobyphone', { method: 'post' })
    async getuserinfobyphone(ctx: Koa.Context) {
      let res:any=  await userService.getuserinfobyphone(ctx.request.body);
        response(ctx, res);
    }

    @router('/getuserqrcode', { method: 'post' })
    async getuserqrcode(ctx: Koa.Context) {
      let res:any=  await userService.GetUserQrCode(ctx.request.body);
        response(ctx, res);
    }

}